package ControlLayer;
import java.sql.SQLException;
import DBLayer.DBCustomer;
import DBLayer.DBSalesOrder;
import DBLayer.IFDBCustomer;
import DBLayer.IFDBSalesOrder;
import ModelLayer.Customer;
import ModelLayer.SalesOrder;
import java.util.Date;

public class CtrSalesOrder {
	public void createNewSalesOrder( Date date, double amount, String deliveryStatus,
			Date deliveryDate, String cpr,Boolean club, int invoiceNo){
		
		IFDBSalesOrder dbSalesOrder = new DBSalesOrder();
		SalesOrder salesOrderObj = new SalesOrder();
		
		salesOrderObj.setSaleID(dbSalesOrder.getMaxSaleID());
		salesOrderObj.setdate(date);
		salesOrderObj.setamount(amount);
		salesOrderObj.setdeliveryStatus(deliveryStatus);
		salesOrderObj.setdeliveryDate(deliveryDate);
		salesOrderObj.setCustomer(new Customer(cpr));
		salesOrderObj.setclub(club);
		salesOrderObj.setinvoiceNo(invoiceNo);
		try{
			dbSalesOrder.createSalesOrder(salesOrderObj);
		}catch(SQLException sql){
			System.out.println("Exception in ctrSalesOrder addProduct SQLEXCEPTION: "+sql);
			sql.printStackTrace();
		}catch(Exception e){
			System.out.println("Exception in ctrSalesOrder addProduct EXCEPTION: "+e);
			e.printStackTrace();
		}
	}
	public Customer findCustomer(String cpr)throws Exception{
		IFDBCustomer dbCustomer = new DBCustomer();
		Customer customer = new Customer();
		try{
			customer = dbCustomer.findCustomer(cpr, false);
		}catch(SQLException sql){
			System.out.println("SQLException in finding customer: "+sql);
			sql.printStackTrace();
		}
		return customer;
	}
	public void createInvoice(int invoiceNo, int saleID, double amount, int discount, int delivery,String paymentDate){
		CtrInvoice ctrInvoice = new CtrInvoice();
		ctrInvoice.createInvoice(invoiceNo, saleID, amount, discount, delivery, paymentDate);
		findSalesOrder(saleID).setinvoiceNo(invoiceNo);
	}
	private SalesOrder findSalesOrder(int saleID){
		IFDBSalesOrder dbSalesOrder = new DBSalesOrder();
		SalesOrder salesOrderObj = new SalesOrder();
		try{
			salesOrderObj = dbSalesOrder.findSalesOrder(saleID);
		}catch(SQLException sql){
			System.out.println("SQLException in finding sales order: "+sql);
			sql.printStackTrace();
		}
		return salesOrderObj;
	}
	public int getMaxSaleID(){
		IFDBSalesOrder salesOrder = new DBSalesOrder();
		return salesOrder.getMaxSaleID();
	}
	public int getLatestSaleID(){
		IFDBSalesOrder salesOrder = new DBSalesOrder();
		return  salesOrder.getLatestSaleID();
	}
	public void confirmSalesOrder(String cpr, Date date, double amount, String deliveryStatus, Date deliveryDate, boolean club, int saleID){
		IFDBSalesOrder dbsalesOrder = new DBSalesOrder();
		SalesOrder salesOrderObj = new SalesOrder();
		Customer customerObj = new Customer();
		try{
			customerObj = findCustomer(cpr);
		}catch(Exception e){
			e.printStackTrace();
		}
		salesOrderObj.setCustomer(customerObj);
		salesOrderObj.setdate(date);
		salesOrderObj.setamount(amount);
		salesOrderObj.setdeliveryStatus(deliveryStatus);
		salesOrderObj.setdeliveryDate(deliveryDate);
		salesOrderObj.setclub(club);
		salesOrderObj.setSaleID(saleID);
		try{
			dbsalesOrder.confirmSalesOrder(salesOrderObj);
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}